package com.powell.health.fragment;



import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.powell.health.activity.ItemListActivity;
import com.powell.health.activity.R;
import com.powell.health.dao.sqlhelper.SqlHealthManagerDatabaseHelper;
//import android.support.v4.app.ListFragment;

/**
 * Created with IntelliJ IDEA.
 * User: tpowell
 * Date: 12/25/12
 * Time: 5:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class CategoryListFragment extends ListFragment {

    boolean isLandscape;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadCategories();
        setOrientation();
        restorePositionState(savedInstanceState);

        if (isLandscape) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(mCurCheckPosition);
        }
    }

    private void restorePositionState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
    }

    private void setOrientation() {
        View detailsFrame = getActivity().findViewById(R.id.details);
        isLandscape = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
    }

    private void loadCategories() {
        String[] categoriesArray = getResources().getStringArray(R.array.categoriesArray);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, categoriesArray));
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    private void showDetails(int index) {
        mCurCheckPosition = index;

        if (isLandscape) {
            getListView().setItemChecked(index, true);
            displayCorrectFragment(index);
        } else {
            launchNewActivity(index);
        }
    }

    private void launchNewActivity(int index) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), ItemListActivity.class);
       // getDatabase().;
        intent.putExtra("index", index);
        startActivity(intent);
    }

    private SQLiteDatabase getDatabase() {
        return this.getActivity().
                openOrCreateDatabase(SqlHealthManagerDatabaseHelper.DATABASE_NAME,
                        SqlHealthManagerDatabaseHelper.DATABASE_VERSION, null);
    }

    private void displayCorrectFragment(int index) {
        ItemDetailsFragment details = (ItemDetailsFragment)
                getFragmentManager().findFragmentById(R.id.details);
        if (details == null || details.getShownIndex() != index) {
            details = ItemDetailsFragment.newInstance(index);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (index == 0) {
                ft.replace(R.id.details, details);
            } else {
          //      ft.replace(R.id.a_item, details);
            }
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }
    }

}
